package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.http.CheckVersionResponse;
import com.innovation.chengzhangriji.ui.widget.CuteButton;
import com.innovation.chengzhangriji.ui.widget.CuteTextView;

public class UpdateDialogActivity extends BaseActivity implements View.OnClickListener {

    private CheckVersionResponse response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_dialog);
        CuteTextView textTitle = (CuteTextView) findViewById(R.id.text_title);
        CuteTextView textContent = (CuteTextView) findViewById(R.id.text_content);
        CuteButton btnDisable = (CuteButton) findViewById(R.id.btn_disable);
        CuteButton btnOk = (CuteButton) findViewById(R.id.btn_ok);
        btnDisable.setOnClickListener(this);
        btnOk.setOnClickListener(this);

        String updateInfo = getIntent().getStringExtra("update_info");
        response = get(updateInfo, CheckVersionResponse.class);
        if (response == null) {
            return;
        }
        String title = response.getVersion() + "新版上线，更新内容如下：";
        textTitle.setText(title);
        String content = response.getDescription();
        content = content.replace("※", "\n");
        textContent.setText(content);
        if (response.getIsCompel() == 1) {
            btnDisable.setVisibility(View.GONE);
        }

    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_disable:
                finish();
                break;
            case R.id.btn_ok:
                // 开启网页下载
                Uri uri = Uri.parse(response.getUrl());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                startActivity(intent);
                if (response.getIsCompel() == 0) {
                    finish();
                }
                break;
        }
    }
}
